package com.zhangye.test.service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhangye.test.mapper.AccountPayMapper;
import com.zhangye.test.model.AccountPay;
import com.zhangye.test.model.Business;
import com.zhangye.test.util.StringUtils;

@Service
public class IosPaySucService {

	Logger logger = LoggerFactory.getLogger(IosPaySucService.class);

	@Autowired
	private BusinessService businessService;

	@Autowired
	private AccountPayMapper account_payMapper;
	
	public Map<String, Object> successNew(Map<String, String> param, Long mId) throws ParseException {
		
		Map<String, Object> results = new HashMap<String, Object>();
		String localizedTitle = "";
		String type = param.get("type");
		String months = param.get("month");
		results.put("status", "1");
		
		if(isNumeric(months)){
			int month = Integer.parseInt(months);
			//更新业务表
			int b = 0;
			Business bus = businessService.selectBymId(mId);
			if (bus == null || bus.equals("")) {
				if (type.equals("dhwx")) { //挂短
					bus.setCardStarttime(new Date());
					bus.setCardEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.getCurrentDateTime(), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));
					bus.setWebsiteStarttime(new Date());
					bus.setWebsiteEndtime(new Date());
					bus.setMarketingStarttime(new Date());
					bus.setMarketingEndtime(new Date());
					localizedTitle = "用于开通挂短业务";
				} else { //微站
					bus.setCardStarttime(null);
					bus.setCardEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.getCurrentDateTime(), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));
					bus.setWebsiteStarttime(new Date());
					bus.setWebsiteEndtime(new Date());
					bus.setMarketingStarttime(null);
					bus.setMarketingEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.getCurrentDateTime(), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));
					localizedTitle = "用于开通挂短、名片业务";
				}
				bus.setDeletelogo(0);
				b = businessService.insert(bus);
			} else {
				Date now = new Date();
				if (type.equals("dhwx")) { //挂短
					Date marketingEndtime = bus.getMarketingEndtime();
					if(marketingEndtime.before(now)){
						bus.setMarketingEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss"), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));	
					}else{
						bus.setMarketingEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.formatDate(marketingEndtime, "yyyy-MM-dd HH:mm:ss"), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));
					}
				} else { //挂短、名片

					Date cardEndTime = bus.getCardEndtime();
					if(cardEndTime.before(now)){
						bus.setCardEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss"), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));	
					}else{
						bus.setCardEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.formatDate(cardEndTime, "yyyy-MM-dd HH:mm:ss"), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));	
					}
					
					Date marketingEndtime = bus.getMarketingEndtime();
					if(marketingEndtime.before(now)){
						bus.setMarketingEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss"), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));	
					}else{
						bus.setMarketingEndtime(StringUtils.format(StringUtils.getDateAdd(month, StringUtils.formatDate(marketingEndtime, "yyyy-MM-dd HH:mm:ss"), "month", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));
					}
				}
				b = businessService.update(bus);
			}
			if (b == 1) {
				results.put("status", "0");
			} else {
				logger.info("交易成功，更新业务表business失败，mId：" + mId);
			}
		}else{
			results.put("status", "1");
		}
		//更新账户变动表
		AccountPay acp = new AccountPay();
		acp.setUserid(mId.intValue());
		acp.setInserttime(new Date());
		acp.setVariation(localizedTitle);
		acp.setPayway("IOS开通业务");
		acp.setOuttradeno(type);
		acp.setDeletelogo(0);
		account_payMapper.insert(acp);
		return results;
	}
	
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			System.out.println(str.charAt(i));
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	
	
}
